/** * Copyright 2013-2016 Guoqiang Chen, Shanghai, China. All rights reserved. * * Author: Guoqiang Chen * Email: subchen@gmail.com * WebURL: https://github.com/subchen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrick.util; import java.util.concurrent.ThreadLocalRandom; public final class RandomStringUtils { private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); private static final char[] BASE62_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); public static String random(int count) { return random(count, false, false); } public static String randomAscii(int count) { return random(count, 32, 127, false, false); } public static String randomAlphabetic(int count) { return random(count, true, false); } public static String randomAlphanumeric(int count) { return random(count, true, true); } public static String randomNumeric(int count) { return random(count, false, true); } public static String randomHex(int count) { return random(count, 0, HEX_CHARS.length, false, false, HEX_CHARS); } public static String randomBase62(int count) { return random(count, 0, BASE62_CHARS.length, false, false, BASE62_CHARS); } public static String random(int count, boolean letters, boolean numbers) { return random(count, 0, 0, letters, numbers); } public static String random(int count, int start, int end, boolean letters, boolean numbers) { return random(count, start, end, letters, numbers, null); } public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) { if (count == 0) { return ""; } else if (count < 0) { throw new IllegalArgumentException("Requested random string length " + count + " is less than 0."); } if (chars != null && chars.length == 0) { throw new IllegalArgumentException("The chars array must not be empty"); } if (start == 0 && end == 0) { if (chars != null) { end = chars.length; } else { if (!letters && !numbers) { end = Integer.MAX_VALUE; } else { end = 'z' + 1; start = ' '; } } } else { if (end <= start) { throw new IllegalArgumentException("Parameter end (" + end + ") must be greater than start (" + start + ")"); } } final char[] buffer = new char[count]; final int gap = end - start; ThreadLocalRandom random = ThreadLocalRandom.current(); while (count-- != 0) { char ch; if (chars == null) { ch = (char) (random.nextInt(gap) + start); } else { ch = chars[random.nextInt(gap) + start]; } if (letters && Character.isLetter(ch) || numbers && Character.isDigit(ch) || !letters && !numbers) { if (ch >= 56320 && ch <= 57343) { if (count == 0) { count++; } else { // low surrogate, insert high surrogate after putting it in buffer[count] = ch; count--; buffer[count] = (char) (55296 + random.nextInt(128)); } } else if (ch >= 55296 && ch <= 56191) { if (count == 0) { count++; } else { // high surrogate, insert low surrogate before putting it in buffer[count] = (char) (56320 + random.nextInt(128)); count--; buffer[count] = ch; } } else if (ch >= 56192 && ch <= 56319) { // private high surrogate, no effing clue, so skip it count++; } else { buffer[count] = ch; } } else { count++; } } return new String(buffer); } public static String random(int count, String chars) { if (chars == null) { return random(count, 0, 0, false, false, null); } return random(count, chars.toCharArray()); } public static String random(int count, char[] chars) { if (chars == null) { return random(count, 0, 0, false, false, null); } return random(count, 0, chars.length, false, false, chars); } }